<?php
namespace app\wxpro\controller;
use think\Db;
use think\request;
use think\Controller;
use app\service\orderService;
use app\service\chargeRuleService;
use app\service\wechatService;
class Wxmsg extends Controller{
	//车辆入场通知
	public function arriveY($openid,$car_number,$parking_name,$in_time,$uname){
        $token = $this->getToken();
        $url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$token;
        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"OsKbRTRxLX5duGmYxBfxGtQQ8yM3DAnPyfBphdyN8uQ", //微信后台的模板信息id
            "page"=>"pages/index/index", 
            "data"=> array(
                "keyword1" => array(
                    "value"=>$car_number,//
                    "color"=>"#173177"
                ),
                "keyword2" => array(
                    "value"=>$parking_name,//
                    "color"=>"#173177"
                ),
				"keyword3" => array(
                    "value"=>$in_time,
                    "color"=>"#173177"
                ),
				"keyword4" => array(
                    "value"=>$parking_name,
                    "color"=>"#173177"
                ),
				"keyword5" => array(
                    "value"=>'',
                    "color"=>"#173177"
                ),
                "keyword6" => array(
                    "value"=>$uname,
                    "color"=>"#173177"
                ),
				"keyword7" => array(
                    "value"=>$uname,
                    "color"=>"#173177"
                ),
				"keyword8" => array(
                    "value"=>"入场",
                    "color"=>"#173177"
                ),
				"keyword9" => array(
                    "value"=>"",
                    "color"=>"#173177"
                ),
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        $data=$this->https_post($url,$post_data);
        return $data;
           
    }
	public function testarrive(){
		$openid="oC2YJwDZNdmP2bqLd95WkakhgzCI";
		
		$car_number="浙A5FZ99";
		$in_time=date('Y-m-d H:i:s');
		$parking_name="汇网停车场";
		$uname="杨战磊";
		return $this->arriveY($openid,$car_number,$parking_name,$in_time,$uname);
	}
	public function test(){
		$openid="ol5hW4zkQ3zAoe_tWpgfY9lxvH9Q";
		$ctime=time()-518400;
		$data=Db::table("wxpro_form")->where("openid",$openid)->where("ctime",'>',$ctime)->find();
		var_dump($data);
	}
	public function testbindcar(){
		$openid="ol5hW4zkQ3zAoe_tWpgfY9lxvH9Q";
		$form_id="219b73a011b64ed586dd185add43591a";
		$car_number="浙A5FZ99";
		$ctime=date('Y-m-d H:i:s');
		return $this->bindcar($openid,$form_id,$car_number,$ctime);
	}
	
	public function testremind(){
		$openid="ol5hW4zkQ3zAoe_tWpgfY9lxvH9Q";
		$form_id="11b45b4f516e4225b15ba44edf524e43";
		$car_number="浙A5FZ99";
		$hours="7小时";
		$days="3天";
		$parking_name="汇网停车场";
		$uname="杨战磊";
		return $this->cardremind($openid,$form_id,$car_number,$parking_name,$hours,$days);
	}
	public function cardremind($openid,$form_id,$car_number,$parking_name,$hours,$days){
		$token = $this->getToken();
        $url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$token;
        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"cRwwTD7S_KQoqySUiaA2hWr2KxTur4GMbeHADyQccd0", //微信后台的模板信息id
			"form_id"=>$form_id,
            "page"=>"pages/package/packageCarList/packageCarList", 
            "data"=> array(
                "keyword1" => array(
                    "value"=>$car_number,//
                    "color"=>"#173177"
                ),
                "keyword2" => array(
                    "value"=>$parking_name,//
                    "color"=>"#173177"
                ),
				"keyword3" => array(
                    "value"=>$hours,
                    "color"=>"#173177"
                ),
				"keyword4" => array(
                    "value"=>$days,
                    "color"=>"#173177"
                ),
				"keyword5" => array(
                    "value"=>'月租车',
                    "color"=>"#173177"
                ),
                "keyword6" => array(
                    "value"=>"您月卡即将到期，请记得及时续费哦～",
                    "color"=>"#173177"
                ),	
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        $data=$this->https_post($url,$post_data);
        return $data;
	}
	//车辆入场通知
	public function arrive($openid,$form_id,$car_number,$parking_name,$in_time,$uname){
        $token = $this->getToken();
        $url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$token;
        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"OsKbRTRxLX5duGmYxBfxGtQQ8yM3DAnPyfBphdyN8uQ", //微信后台的模板信息id
			"form_id"=>$form_id,
            "page"=>"pages/index/index", 
            "data"=> array(
                "keyword1" => array(
                    "value"=>$car_number,//
                    "color"=>"#173177"
                ),
                "keyword2" => array(
                    "value"=>$parking_name,//
                    "color"=>"#173177"
                ),
				"keyword3" => array(
                    "value"=>$in_time,
                    "color"=>"#173177"
                ),
				"keyword4" => array(
                    "value"=>$parking_name,
                    "color"=>"#173177"
                ),
				"keyword5" => array(
                    "value"=>'',
                    "color"=>"#173177"
                ),
                "keyword6" => array(
                    "value"=>$uname,
                    "color"=>"#173177"
                ),
				"keyword7" => array(
                    "value"=>$uname,
                    "color"=>"#173177"
                ),
				"keyword8" => array(
                    "value"=>"入场",
                    "color"=>"#173177"
                ),
				"keyword9" => array(
                    "value"=>"",
                    "color"=>"#173177"
                ),
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        $data=$this->https_post($url,$post_data);
        return $data;
           
    }
	//车辆绑定通知
    public function bindcar($openid,$form_id,$car_number,$ctime){
        $token = $this->getToken();
        $url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$token;
        $post_data = array(
            "touser"=>$openid, //推送给谁,openid
            "template_id"=>"r4AVUHPyXTbosrBgTRFJw1OcGeuWh4JwIeboZAEiKyA", //微信后台的模板信息id
			"form_id"=>$form_id,
            "page"=>"pages/index/index", 
            "data"=> array(
                
                "keyword1" => array(
                    "value"=>$car_number,//
                    "color"=>"#173177"
                ),
                "keyword2" => array(
                    "value"=>$ctime,//
                    "color"=>"#173177"
                ),
				"keyword3" => array(
                    "value"=>"成功",
                    "color"=>"#173177"
                ),
				"keyword4" => array(
                    "value"=>"123456",
                    "color"=>"#173177"
                ),
                
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        $data=$this->https_post($url,$post_data);
        return $data;
           
    }

    public function https_post($url,$data)
    {
        $curl = curl_init();
        
        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        if (curl_errno($curl)) {
           return 'Errno'.curl_error($curl);
        }
        curl_close($curl);
        return $result;
    }
	public function curl_get($url, $data_type='text')

    {

        $cl = curl_init();

        if(stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }        

    }
	
	
	//获取accesstoken
    public function getToken(){
		$appid = "wx6e64e3c7372be385";//小程序的唯一标识
        $secret = "739ec2a4762e9e9a983107f2e513aff7";//小程序的AppSecret
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $res = $this->curl_get($url);
        $res = json_decode($res,1);
        return $res['access_token'];
    }

	public function index(){
		if(request()->has("echostr")){
			$signature = request()->get("signature"); 
   			$timestamp = request()->get("timestamp"); 
    		$nonce = request()->get("nonce"); 
   			$token = "zaozao201988"; 
			if($this->checkSignature($signature,$timestamp,$nonce,$token))
			{
				echo request()->get("echostr"); 
      			exit; 
			}
		}else{
			
		}
	}

	//生成签名
  	private function checkSignature($signature,$timestamp,$nonce,$token) 
  	{ 
    	$tmpArr = array($token, $timestamp, $nonce); 
    	sort($tmpArr,SORT_STRING); 
    	$tmpStr = implode($tmpArr); 
    	$tmpStr = sha1($tmpStr); 
    	if( $tmpStr == $signature ){ 
      		return true; 
    	}else{ 
      		return false; 
    	} 
  	} 

    
}